const std = @import("std");
const expect = std.testing.expect;

const pipesLib = @import("../pipes.zig");
const Pipe = @import("../pipe.zig").Pipe;
const PipeType = @import("../pipe.zig").PipeType;


test "create and free" {
	var pipes = pipesLib.Pipes{
		.allocator = std.testing.allocator
	};
	pipes.init(10);
	defer pipes.deinit();

	try pipes.add_pipe(2, 2, PipeType.HORIZONTAL);
}


test "move pipe" {
	var pipes = pipesLib.Pipes{
		.allocator = std.testing.allocator
	};
	pipes.init(10);
	defer pipes.deinit();

	const old_x: u8 = 2;
	const old_y: u8 = 3;
	try pipes.add_pipe(old_x, old_y, PipeType.HORIZONTAL);
	const pipe: *Pipe = pipes.get_pipe(old_x, old_y).?;

	const new_x: u8 = 6;
	const new_y: u8 = 1;
	const new_pipe: *Pipe = try pipes.move_pipe(pipe, new_x, new_y);

	const old_pipe: ?*Pipe = pipes.get_pipe(old_x, old_y);

	try expect(new_pipe.x == new_x);
	try expect(new_pipe.y == new_y);
	try expect(old_pipe == null);
}


test "move untracked pipe" {
	var pipes = pipesLib.Pipes{
		.allocator = std.testing.allocator
	};
	pipes.init(10);
	defer pipes.deinit();

	const untracked_x: u8 = 1;
	const untracked_y: u8 = 2;
	var untracked_pipe = Pipe{
		.x = untracked_x,
		.y = untracked_y,
		.type = PipeType.VERTICAL
	};

	const move_x: u8 = 6;
	const move_y: u8 = 1;
	const moved_pipe = try pipes.move_pipe(&untracked_pipe, move_x, move_y);

	const fetched_untracked_pipe: ?*Pipe =
		pipes.get_pipe(untracked_x, untracked_y);

	try expect(moved_pipe == &untracked_pipe);
	try expect(moved_pipe.x == untracked_x);
	try expect(moved_pipe.y == untracked_y);
	try expect(fetched_untracked_pipe == null);
}
